//Alisa Burdeyny

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;

import com.sun.opengl.util.GLUT;

public class DrawTopViewWalker {

	Sections neck = new Sections(0, .2);
	Sections leftArm = new Sections(90, 1.75);
	Sections rightArm = new Sections(90, 1.75);
	Sections leftForearm = new Sections(0, 1.5);
	Sections rightForearm = new Sections(0, 1.5);
	Sections shoulders = new Sections(0, 1.75);
	Sections torso = new Sections (0, 2.5);
	Sections pelvis = new Sections(0, 1);
	Sections leftThigh = new Sections(90, 1.5);
	Sections rightThigh = new Sections(90, 1.5);
	Sections leftShin = new Sections(90, 1.75);
	Sections rightShin = new Sections(90, 1.75);
	Sections leftFoot = new Sections(0, .25);
	Sections rightFoot = new Sections(0, .25);
	double maxHeight, floorHeight, bounceDist;

	//set the angles of the sections of the walker based on calculations done in the Walk class
	public void MoveWalker (double lTh, double rTh, double lSh, double rSh, double lA, double rA, double lFa, double rFa)
	{
		leftThigh.setAngle(lTh);
		rightThigh.setAngle(rTh);
		leftShin.setAngle(lSh);
		rightShin.setAngle(rSh);
		leftArm.setAngle(lA);
		rightArm.setAngle(rA);
		leftForearm.setAngle(lFa);
		rightForearm.setAngle(rFa);
	}

	//draw the walker
	public void drawIt(GLAutoDrawable drawable, double distMoveX, double distMoveZ, double walkAng, int teamPlayer)
	{

		GL gl = drawable.getGL();            // get JOGL Drawable object
		GLU glu = new GLU();
		GLUT glut = new GLUT();

		//find the height from the bottom of the foot closest to the ground to the top of the pelvis
		maxHeight = Math.max(-leftThigh.getLength()*Math.sin(Math.toRadians(leftThigh.getAngle()))-leftShin.getLength()*Math.sin(Math.toRadians(leftShin.getAngle()))-leftFoot.getLength()-.1, -rightThigh.getLength()*Math.sin(Math.toRadians(rightThigh.getAngle()))-rightShin.getLength()*Math.sin(Math.toRadians(rightShin.getAngle()))-rightFoot.getLength()-.1);
		//find the distance from the initial height of the walker at rest to the floor
		floorHeight = -.25-leftThigh.getLength()-leftShin.getLength()-leftFoot.getLength();

		if(teamPlayer == 1)
			gl.glColor3d(1,0,1);
		else if(teamPlayer == 2)
			gl.glColor3d(1, 0, 0);
		else if(teamPlayer == 3)
			gl.glColor3d(0, 0, 1);
		else
			gl.glColor3d(1,1,0);

		/****lower body*****/
		//draw left thigh
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);;
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX, 0, -pelvis.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(leftThigh.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, leftThigh.getLength(), 4, 4);

		//draw right thigh
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX, 0, pelvis.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(rightThigh.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, rightThigh.getLength(), 4, 4);   

		//draw left shin
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX+leftThigh.getLength()*Math.cos(Math.toRadians(leftThigh.getAngle())), -leftThigh.getLength()*Math.sin(Math.toRadians(leftThigh.getAngle()))-.25*Math.sin(Math.toRadians(leftThigh.getAngle())), -pelvis.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(leftShin.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, leftShin.getLength(), 4, 4);

		//draw right shin
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX+rightThigh.getLength()*Math.cos(Math.toRadians(rightThigh.getAngle())), -rightThigh.getLength()*Math.sin(Math.toRadians(rightThigh.getAngle()))-.25, pelvis.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(rightShin.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, rightShin.getLength(), 4, 4);

		/****upper body****/

		//draw torso
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-90, 1, 0, 0);
		gl.glScaled(1, 3, 1);
		glut.glutSolidCylinder(.25, torso.getLength(), 4, 4);

		//draw right arm
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX, torso.getLength(), shoulders.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(rightArm.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, rightArm.getLength(), 4, 4);

		//draw left arm
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX, torso.getLength(), -shoulders.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(leftArm.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, leftArm.getLength(), 4, 4);

		//draw right forearm
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX+rightArm.getLength()*Math.cos(Math.toRadians(rightArm.getAngle())), torso.getLength()-rightArm.getLength()*Math.sin(Math.toRadians(rightArm.getAngle())), shoulders.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(-90+rightArm.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, rightForearm.getLength(), 4, 4);

		//draw right forearm
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX+leftArm.getLength()*Math.cos(Math.toRadians(leftArm.getAngle())), torso.getLength()-leftArm.getLength()*Math.sin(Math.toRadians(leftArm.getAngle())), -shoulders.getLength()/2.0+distMoveZ);
		gl.glRotated(90, 0, 1, 0);
		gl.glRotated(-90+leftArm.getAngle(), 1, 0, 0);
		glut.glutSolidCylinder(.25, leftForearm.getLength(), 4, 4);

		//draw right hand
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glColor3d(1,1,1);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX+rightArm.getLength()*Math.cos(Math.toRadians(rightArm.getAngle()))+rightForearm.getLength()*(Math.sin(Math.toRadians(rightArm.getAngle()))), torso.getLength()-rightArm.getLength()*Math.sin(Math.toRadians(rightArm.getAngle()))+rightForearm.getLength()*(Math.cos(Math.toRadians(rightArm.getAngle()))), shoulders.getLength()/2.0+distMoveZ);
		glut.glutSolidSphere(.325, 4, 4);

		//draw left hand
		gl.glLoadIdentity();
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		gl.glRotated(-walkAng, 0, 1, 0);
		gl.glTranslated(-distMoveX, 0, -distMoveZ);
		gl.glTranslated(distMoveX+leftArm.getLength()*Math.cos(Math.toRadians(leftArm.getAngle()))+leftForearm.getLength()*(Math.sin(Math.toRadians(leftArm.getAngle()))), torso.getLength()-leftArm.getLength()*Math.sin(Math.toRadians(leftArm.getAngle()))+leftForearm.getLength()*(Math.cos(Math.toRadians(leftArm.getAngle()))), -shoulders.getLength()/2.0+distMoveZ);
		glut.glutSolidSphere(.325, 4, 4);

		//draw floor
		gl.glLoadIdentity();
		gl.glTranslated(0, floorHeight, 0);
		gl.glColor3d(0, .7, .3);
		gl.glBegin(GL.GL_QUADS);
		gl.glVertex3d(500, 0, 500);
		gl.glVertex3d(500, 0, -500);
		gl.glVertex3d(-500, 0, -500);
		gl.glVertex3d(-500, 0, 500);
		gl.glEnd();

		//draw white lines
		gl.glLoadIdentity();
		gl.glRotated(45, 0, 1, 0);
		gl.glColor3d(1, 1, 1);
		gl.glBegin(GL.GL_QUADS);
		gl.glVertex3d(115, 0, -500);
		gl.glVertex3d(116, 0, -500);
		gl.glVertex3d(116, 0, 500);
		gl.glVertex3d(115, 0, 500);
		gl.glEnd();

		gl.glBegin(GL.GL_QUADS);
		gl.glVertex3d(-115, 0, -500);
		gl.glVertex3d(-116, 0, -500);
		gl.glVertex3d(-116, 0, 500);
		gl.glVertex3d(-115, 0, 500);
		gl.glEnd();
	}
	
	public void drawFrisbee(GLAutoDrawable drawable, double distMoveX, double distMoveZ)
	{
		GL gl = drawable.getGL();            // get JOGL Drawable object
		GLU glu = new GLU();
		GLUT glut = new GLUT();
		
		gl.glLoadIdentity();
		gl.glColor3d(0, .75, 1);
		gl.glRotated(-135, 0, 1, 0);
		gl.glTranslated(distMoveX, 0, distMoveZ);
		glut.glutSolidSphere(1.5, 4, 4);
	}
}